<template>
  <view class="content screen-width">
    <view class="title_head">
      <view class="title_item">
        <ul>
          <li
            @tap="changeIndex(index)"
            :class="currentItem === index ? 'active' : ''"
            v-for="(item, index) in titleItem"
            :key="index"
          >{{ item }}</li>
        </ul>
      </view>
      <view class="title_search">
        <view class="search_item">
          <input type="text" placeholder="请输入您想要查询的关键词" v-model="searchKey" />
          <image src="../../static/proty/search_icon.png" />
        </view>
      </view>
    </view>
    <view class="areaList">
      <view @tap="showArea">
        <input type="text" :value="address ? address : '地区'" />
        <image src="../../static/proty/botton_icon.png" />
      </view>
      <view @tap="showtime">
        <input type="text" :value="time ? time : '发布时间'" />
        <image src="../../static/proty/botton_icon.png" />
      </view>
    </view>

    <scroll-view scroll-y class="content_main" @scrolltolower="scrolltolower">
      <ul v-show="currentItem === 0">
        <!-- 成果 -->
        <navigator
          redirect
          :url="`/pages/result/achievDetail?id=${item.id}`"
          v-for="(item, index) in resultList"
          :key="index"
          class="listItem"
        >
          <view class="title" v-html="item.achievementsName"></view>
          <view class="center">
            <view class="time multipleRow">{{ item.createTime }}</view>
          </view>
        </navigator>
      </ul>
      <ul v-show="currentItem === 1">
        <!-- 需求 -->
        <navigator
          redirect
          :url="`/pages/result/demandDetail?id=${item.id}`"
          class="listItem"
          v-for="(item, index) in demandList"
          :key="index"
        >
          <view class="title">
            <p v-html="item.name"></p>
          </view>
          <view class="center">
            <view class="time multipleRow">{{ item.createTime }}</view>
          </view>
        </navigator>
      </ul>
      <!-- 没有数据的提示 -->
      <van-divider>我也是有底线的</van-divider>
    </scroll-view>

    <view class="btn-box screen-width">
      <view v-show="currentItem === 0" class="publish" @click="toAdd('achievAdd')">发布科技成果</view>
      <view v-show="currentItem === 1" class="publish" @click="toAdd('demandAdd')">发布成果需求</view>
    </view>

    <!-- 地区 -->
    <resultArea
      ref="picker"
      :isshowArea="isshowArea"
      :parent="parent"
      :children="children"
      @affirm="affirm"
      @cancel="cancel"
    ></resultArea>
    <!-- 发布时间 -->
    <van-popup v-model="isshowTime" position="bottom" :style="{ height: '50%' }">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择发布时间"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="onConfirmTimes"
        @cancel="cancel"
      />
    </van-popup>
    <Festival></Festival>
  </view>
</template>

<script>
import {
  achievementsList,
  demandList,
  resultAreaInfo
} from "../../util/api/result.js";
import resultArea from "../../components/ResultArea.vue";
import { isWeixin, wxShareConfig } from "@/util/wx.js";
export default {
  components: { resultArea },
  data() {
    return {
      titleItem: ["科技成果", "成果需求"],
      resultList: [],
      demandList: [],
      areaArr: [],
      parent: [],
      children: [],
      searchKey: "",

      total: 0,
      pageNo1: 1,
      pageNo2: 1,
      province: "",
      city: "",
      currentItem: 0,
      isshowArea: false,
      address: "", // 选中的地区(展示)
      isshowTime: false,
      time: "", //展示数据(时间)

      minDate: new Date(2000, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date()
    };
  },
  onLoad() {
    // this.address = uni.getStorageSync("city");
    this.getArea();
    this.getReaultList();
    this.initShareConfig();
  },
  methods: {
    changeIndex(index) {
      this.currentItem = index;
      // this.province = "";
      // this.city = "";
      this.time = "";
      // this.address = "";
      this.getData();
    },
    getArea() {
      resultAreaInfo().then(res => {
        this.areaArr = res.result;
        this.areaArr.map(item => {
          this.parent.push(item.value);
          // let cityArr = "请选择";
        });
        this.areaArr.map(item => {
          this.children.push(item.children);
        });
        // console.log(8888888, this.children);
        this.parent.unshift("请选择");
        for (let i = 0; i < this.children.length; i++) {
          this.children[i].unshift({ value: "请选择" });
        }
        // console.log(9999999, this.parent, this.children);
      });
    },
    getReaultList() {
      let params = {
        pageNo: this.pageNo1,
        pageSize: 10,
        province: this.province,
        city: this.city,
        achievementsName: this.searchKey,
        createTime: this.time
      };
      // if (!params.city && !params.province) {
      //   params.city = uni.getStorageSync("city");
      //   params.province = uni.getStorageSync("province");
      // }
      achievementsList(params).then(res => {
        // console.log(res);
        this.resultList.push(...res.result.records);
        this.$changeText(res.result.records, "title", this.searchKey);
        this.total = res.result.total;
      });
    },
    getDemandList() {
      let params = {
        pageNo: this.pageNo2,
        pageSize: 10,
        province: this.province,
        city: this.city,
        name: this.searchKey,
        createTime: this.time
      };
      // if (!params.city && !params.province) {
      //   params.city = uni.getStorageSync("city");
      //   params.province = uni.getStorageSync("province");
      // }
      demandList(params).then(res => {
        // console.log(res);
        this.demandList.push(...res.result.records);
        this.$changeText(res.result.records, "title", this.searchKey);
        this.total = res.result.total;
      });
    },
    showArea() {
      this.isshowArea = true;
    },
    affirm(parentArrIndex, childrenArrIndex) {
      if (parentArrIndex === 0) {
        this.province = "";
        this.city = "";
        this.address = "";
      } else if (
        parentArrIndex !== 0 &&
        childrenArrIndex !== undefined &&
        childrenArrIndex !== "" &&
        childrenArrIndex !== 0
      ) {
        this.province = this.parent[parentArrIndex];
        let arr = this.children[parentArrIndex - 1];
        this.city = arr[childrenArrIndex].value;
      } else {
        this.province = this.parent[parentArrIndex];
        this.city = "";
      }
      if (this.province && this.city) {
        this.address = this.city;
      } else {
        this.address = this.province;
      }
      this.getData();
      this.cancel();
    },
    getData() {
      if (this.currentItem === 0) {
        this.resultList = [];
        this.pageNo1 = 1;
        this.getReaultList();
      } else {
        this.demandList = [];
        this.pageNo2 = 1;
        this.getDemandList();
      }
    },
    cancel() {
      this.isshowArea = false;
      this.isshowTime = false;
    },
    showtime() {
      this.isshowTime = true;
    },
    onConfirmTimes(v) {
      console.log(999999999, v);
      this.time = this.timeFormat(v);
      this.getData();
      this.cancel();
    },
    timeFormat(time) {
      // 时间格式化 2019-09-08
      let year = time.getFullYear();
      let month = (time.getMonth() + 1).toString().padStart(2, "0");
      let day = time
        .getDate()
        .toString()
        .padStart(2, "0");
      return year + "-" + month + "-" + day;
    },
    scrolltolower() {
      if (this.currentItem === 0 && this.resultList.length >= this.total) {
        return;
      } else if (
        this.currentItem === 1 &&
        this.demandList.length >= this.total
      ) {
        return;
      }
      if (this.currentItem === 0) {
        this.pageNo1 = this.pageNo1 + 1;
        this.getReaultList();
      } else if (this.currentItem === 1) {
        this.pageNo2 = this.pageNo2 + 1;
        this.getDemandList();
      }
    },
    // 发布
    toAdd(url) {
      let token = this.$getToken();
      if (!token) {
        uni.showToast({
          title: "您还没有登录，请登录后操作",
          duration: 2000,
          icon: "none"
        });
        setTimeout(() => {
          uni.setStorageSync("pageUrl", window.location.href);
          uni.navigateTo({
            url: "/pages/login/index"
          });
        }, 2000);
        return;
      }
      uni.navigateTo({
        url: `/pages/result/${url}`
      });
    },
    initShareConfig() {
      let _this = this;
      let windowurl = window.location.href.split("#")[0];
      uni.request({
        url: "https://oldm.51bmj.cn/WeiXin/GetClientSign", //仅为示例，并非真实接口地址。
        data: {
          rawUrl: windowurl
        },
        dataType: "json",
        header: {
          "X-Requested-With": "XMLHttpRequest"
        },
        method: "GET",
        success: res => {
          if (res.statusCode == 200) {
            let { data } = res;
            let shareInfo = {
              title: "全国最新科技成果",
              desc: "一键发布，无缝对接",
              link: window.location.href
            };
            wxShareConfig(this.$wx, data, shareInfo);
          }
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.popup {
  height: fit-content;
  width: 100%;
  background: #fff;
}

.picker-btn {
  display: flex;
  height: 100upx;
  width: 100%;
  line-height: 100upx;
  background: #fff;
  font-size: 17px;
  z-index: 1;
  border-bottom: 1rpx solid #f8f8f8;

  .left {
    flex: 1;
    color: #0076ff;
    padding-left: 40upx;
    box-sizing: border-box;
  }

  .right {
    flex: 1;
    text-align: right;
    padding-right: 40upx;
    color: #fe4533;
    box-sizing: border-box;
  }
}

picker-view {
  width: 100%;
  height: 500rpx;
  display: relative;
}
.item {
  line-height: 100rpx;
  text-align: center;
}
.title_head {
  padding: 20rpx 30rpx;
  background-color: #2862ad;

  .title_item {
    display: flex;
    align-items: center;
    height: 100rpx;

    ul {
      display: flex;
      align-items: center;
      width: 100%;
      li {
        position: relative;
        font-size: 14px;
        color: #aaa;
        text-align: center;
        width: 30%;
        &.active {
          color: #fff;
          font-size: 16px;
          &::before {
            content: "";
            position: absolute;
            left: 40%;
            bottom: -25rpx;
            width: 30rpx;
            height: 3px;
            border-radius: 7rpx;
            background-color: #44a2ff;
          }
        }
      }
    }
  }

  .title_search {
    padding: 20rpx auto;
    height: 120rpx;
    align-items: center;
    display: flex;

    .search_item {
      height: 40px;
      width: 100%;
      display: flex;
      background-color: #fff;
      align-items: center;
      border-radius: 14rpx;
    }

    input {
      flex: 1;
      font-size: 14px;
      padding: 0 10rpx;
      padding-left: 30rpx;
      height: 30rpx;
      &::placeholder {
        font-size: 12px;
      }
    }

    image {
      max-width: 16px;
      max-height: 16px;
      margin: 0 30rpx;
    }
  }
}

.content_main {
  height: calc(100vh - 380rpx - 50px);

  padding-bottom: 60rpx;
  ul {
    .listItem {
      border-radius: 10rpx;
      background-color: #fff;
      margin: 20rpx 30rpx;
      padding: 30rpx 40rpx;
      position: relative;
      .title {
        color: #000;
        font-weight: 700;
        font-size: 16px;
      }

      .center {
        display: flex;
        margin-top: 20rpx;
        justify-content: space-between;

        .area {
          flex: 1;
          padding-right: 20rpx;
          color: #aaa;
          font-size: 10px;
        }

        .time {
          text-align: right;
          flex: 1;
          color: #aaa;
          font-size: 10px;
        }
      }
      img {
        position: absolute;
        top: 6rpx;
        right: 8rpx;
      }
    }

    li {
      border-radius: 10rpx;
      background-color: #fff;
      margin: 20rpx 30rpx;
      padding: 30rpx 40rpx;

      .title {
        color: #000;
        font-weight: 700;
      }

      .center {
        display: flex;
        margin-top: 20rpx;
        justify-content: space-between;

        .area {
          flex: 1;
          padding-right: 20rpx;
          color: #aaa;
          font-size: 10px;
        }

        .time {
          flex: 1;
          color: #aaa;
          font-size: 10px;
        }
      }
    }
  }
}

.btn-box {
  position: fixed;
  bottom: 0;
  width: 100%;

  .publish {
    width: 100%;
    padding: 25rpx 0;
    text-align: center;
    background: #1678ff;
    color: white;
    font-size: 32upx;
  }
}

.areaList {
  padding: 0 20rpx;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  height: 60px;
  overflow: scroll;
  view {
    max-height: 80rpx;
    background-color: #fff;
    margin-right: 30rpx;
    display: flex;
    padding: 10rpx 10rpx;
    align-items: center;
    width: 22%;

    image {
      width: 20rpx;
      height: 14rpx;
      margin-left: 20rpx;
    }

    input {
      width: 140rpx;
      font-size: 10px;
      color: #44a2ff;
    }
  }
}

/deep/ .van-dropdown-menu__bar {
  display: flex;
  align-items: center;
  background-color: #f8f9fd;
  padding-left: 30rpx;
}

/deep/ .van-dropdown-menu__item {
  flex: none;
  padding: 10rpx 30rpx;
  text-align: center;
  height: 56rpx;
  background-color: #fff;
  margin-right: 20rpx;

  span {
    color: #77b0ff;
    font-size: 14px;
  }
}
</style>

